(function(){

YAHOO.pendular.PropertiesLoader = function(config){
    if (!config) {
        config = {};
    }
};

YAHOO.lang.extend(YAHOO.pendular.PropertiesLoader, Object, {
    getConfig: function(config) {
        var basename = config.basename, 
            locale = config.locale, 
            language, country, name
        ;
        if (!basename){ 
            basename = "settings";
            config.basename = basename;
        }
        if (locale) {
            if (locale.indexOf("_")!==-1) {
                locale = config.locale.split("_");
                language = locale[0];
                country = locale[1];
            }
            else {
                language = locale;
                country = null;
            }
        }
        else {
            language = null;
            country = null;
        }
        config.language = language;
        config.country = country;
        config.name = basename + 
                (language ? "_" + language + (country ? "_" + country : "") : "") + 
                ".properties"
        ;
    },
    parse: function(text){
        var settings = {};
        text = text.replace(/\r/g, "\n").replace(/\n+/g, "\n");
        var lines = text.split("\n"),
            numLines = lines.length,
            line, i,
            indexOfEquals, name, value;
        for (i=0; i<numLines; i++){
            line = lines[i];
            line = line.replace(/#.*$/, "");
            indexOfEquals = line.indexOf("=");
            switch (indexOfEquals) {
                case 0:
                case -1:
                    break;
                default:
                    name = line.substr(0, indexOfEquals);
                    value = line.substr(indexOfEquals + 1);
            }
            settings[name] = value;
        }
        return settings;
    },
    load: function(config){        
        var me = this,
            callback = config.callback;
        if (callback && !callback.scope) {
            callback.scope = window;
        }
        this.getConfig(config);
        YAHOO.util.Connect.asyncRequest("GET", config.name, {
            success: function(o){
                try {
                    if (callback && callback.success) {
                        callback.success.call(
                            callback.scope, me, o,
                            me.parse(o.responseText)
                        );
                    }
                } catch (ex) {
                    if (callback && callback.failure) {
                        callback.failure.call(callback.scope, me, o);
                    }
                }
            },
            failure: function(o){
                if (config.country) {
                    config.locale = config.language;
                    me.load(config);
                }
                else
                if (config.language) {
                    config.locale = null;
                    me.load(config);
                }
                else
                if (callback && callback.failure) {
                    callback.failure.call(callback.scope, me, o);
                }
            }
        });        
    }
});

})();
